package mypackage;

import mypackage.domain.Person;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.LoggerFactory;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Test1 {

    public static void main(String[] args) {
        //test1(); // 测试去重
        //test2();
        //test3();
        //test4();
        //test5();
        test6();
    }

    /**
     * 需求：有一个Person对象集合，每一个Person对象都有用户名、地址，是否为预付费用户和注册时间
     * 现在想实现的效果是按天统计注册的用户数，形成一个map集合，如下示例
     * 示例：{2022-07-05=2,2022-07-06=1,2022-07-07=1}
     */
    private static void test6() {

        Person person1 = new Person("shg1", "x1", false, new Date("2022/07/05 1:25:22"));
        Person person2 = new Person("shg2", "x1", false, new Date("2022/07/05 11:25:22"));
        Person person3 = new Person("shg3", "x1", false, new Date("2022/07/06 1:25:22"));
        Person person4 = new Person("shg4", "x2", false, new Date("2022/07/07 1:25:22"));

        List<Person> personList = new ArrayList<>();
        personList.add(person1);
        personList.add(person2);
        personList.add(person3);
        personList.add(person4);

        Map<String, Long> collect1 = personList.stream()
                .map(person -> TimestampUtils.dateToStrByUnit("day", person.getRigisterDate()))
                .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
        System.out.println(collect1);

        //Stream<String> day = personList.stream().map(person -> TimestampUtils.dateToStrByUnit("day", person.getRigisterDate()));
        //day.forEach(System.out::println);
        //Map<String, Long> collect = day.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));

    }


    private static void test5() {
        boolean isGet = false;
        System.out.println(isGet ? "get" : "post");
    }

    private static void test1() {
        String str = "1.1.1.1,2.2.2.2,";
        String substring = str.substring(0, str.length() - 1);
        System.out.println("substring = " + substring);
    }

    private static void test2() {
        // 60_173_17_6, 36_32_174_26, 96_46_29_171, 96_46_19_113, 54_82_170_254
        Set<String> existedWhiteList = new HashSet<>();
        existedWhiteList.add("54_82_170_254"); // 去掉
        existedWhiteList.add("60_173_17_6,36_32_174_26,96_46_29_171,96_46_19_113,54_82_170_254"); // 去掉60_173_17_6 和  54_82_170_254
        existedWhiteList.add("36_161_57_223");
        existedWhiteList.add("96_46_29_171");
        existedWhiteList.add("60_173_17_6"); // 去掉
        existedWhiteList.add("10_50_0_71");
        existedWhiteList.add("10_50_0_1");
        existedWhiteList.add("35_174_29_43");

        String userIp = "54.82.170.254,60.173.17.6,36.32.174.26,96.46.19.113,1.1.1.1";

        String result = deduplicationIp(userIp, existedWhiteList);
        System.out.println("result = " + result);

    }

    private static String deduplicationIp(String userIp, Set<String> existedWhiteList) {
        //1. 整理 existedWhiteList，存放到一个新set集合中，集合中存放的是 单独的ip地址

        Set<String> tempSet = new HashSet<>();
        for (String s : existedWhiteList) {
            String[] split = s.split(",");
            for (String s1 : split) {
                tempSet.add(s1);
            }
        }

        String tempUserIp = userIp.replaceAll("\\.", "_");
        String[] userIps = tempUserIp.split(",");
        String result = "";
        for (String ip : userIps) {
            if (!tempSet.contains(ip)) {
                result = result + ip + ",";
            }
        }

        String res = result.replaceAll("_", "\\.");
        existedWhiteList.add(res);
        if (existedWhiteList != null)
            if (StringUtils.isNoneBlank(res)) {
                res = res.substring(0, result.length() - 1);
            }
        return res;
    }

    private static void test3() {
        String ip = "54.82.170.254,60.173.17.6,54.82.170.254";
        extracted(ip);
    }

    private static void extracted(String ip) {
        String res = ip.replaceAll("\\.", "_");
        String[] split = res.split(",");
        for (String s : split) {
            System.out.println("..." + s);
        }
        System.out.println("res = " + res);
    }

    public static void test4() {
        fun1();
    }

    public static void fun1(String... array) {
        System.out.println(array.length);
        Arrays.sort(array);
    }


}
